package com.ssms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ssms.common.R;
import com.ssms.domain.Course;
import com.ssms.domain.ExamScore;
import com.ssms.domain.Student;
import com.ssms.mapper.CourseMapper;
import com.ssms.mapper.ExamScoreMapper;
import com.ssms.service.StudentService;
import com.ssms.service.TeacherService;
import com.ssms.utils.JwtUtils;
import com.ssms.vo.LoginVo;
import com.ssms.vo.ScoreInfoVo;
import com.ssms.vo.StudentInfoVo;
import com.ssms.vo.paras.PostStudentInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private ExamScoreMapper examScoreMapper;

    @Autowired
    private CourseMapper courseMapper;

    @GetMapping("/getStudentInfo")
    public R<Object> getStudentInfo(@RequestHeader String token) throws Exception{

        Claims claims = JwtUtils.getClaims(token);
        int id = (int)claims.get("id");
        String name = (String) claims.get("name");
        String role = (String) claims.get("role");
        String major = (String) claims.get("major");
        String tclass = (String) claims.get("class");
        String num = (String) claims.get("number");
        StudentInfoVo st = new StudentInfoVo();
        st.setMajor(major);
        st.setName(name);
        st.setTclass(tclass);
        st.setStuId(num);
        st.setRole(role);
        return R.success(st,"获取成功");
    }

    @PostMapping("/poststudentinfo")
    public R<Object> poststudentinfo(@RequestHeader String token, @RequestBody PostStudentInfo postStudentInfo) throws Exception{

        Claims claims = JwtUtils.getClaims(token);
        int id = (int)claims.get("id");
        int counsellorId = (int) claims.get("counsellor_id");
        Student st = new Student();
        st.setStudentId(id);
        st.setAcademy(postStudentInfo.getAcademy());
        st.setTelephone(postStudentInfo.getTelephone());
        st.setStudentName(postStudentInfo.getName());
        st.setStudentNum(postStudentInfo.getEmail());
        st.setStudentClass(postStudentInfo.getTclass());
        st.setRole("student");
        st.setGender(postStudentInfo.getGender());
        st.setBorndate(postStudentInfo.getBorndate());
        st.setMajor(postStudentInfo.getMajor());
        studentService.saveOrUpdate(st);

        Map tokenMap = new HashMap();
        tokenMap.put("id",id);
        tokenMap.put("name",postStudentInfo.getName());
        tokenMap.put("class",st.getStudentClass());
        tokenMap.put("number",st.getStudentNum());
        tokenMap.put("role",st.getRole());
        tokenMap.put("counsellor_id",counsellorId);
        tokenMap.put("major",st.getMajor());
        tokenMap.put("gender",st.getGender());
        tokenMap.put("borndate",st.getBorndate());
        tokenMap.put("telephone",st.getTelephone());
        tokenMap.put("academy",st.getAcademy());

        String token1 = JwtUtils.getToken(tokenMap);
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token1);

        return R.success(loginVo,"修改成功");
    }
    @GetMapping("/studentgrade")
    public R<Object> studentgrade(@RequestHeader String token) throws Exception{

        Claims claims = JwtUtils.getClaims(token);
        int id = (int)claims.get("id");
        LambdaQueryWrapper<ExamScore> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamScore::getSId,id);
        List<ExamScore> examScoreList = examScoreMapper.selectList(queryWrapper);
        List<ScoreInfoVo> scoreInfoVoList = new ArrayList<>();
        for (ExamScore examScore : examScoreList) {
            LambdaQueryWrapper<Course> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Course::getCourseId,examScore.getCId());
            Course course = courseMapper.selectOne(queryWrapper1);
            ScoreInfoVo scoreInfoVo = new ScoreInfoVo();
            scoreInfoVo.setCredit(examScore.getCredit());
            scoreInfoVo.setTestClass(examScore.getTestClass());
            scoreInfoVo.setPositiveexam(examScore.getPositiveScore());
            scoreInfoVo.setRetakeexam(examScore.getRetakeScore());
            scoreInfoVo.setSubjects(course.getCourseName());
            scoreInfoVoList.add(scoreInfoVo);
        }

        return R.success(scoreInfoVoList,"获取成功");
    }

}
